package tweeks.category.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tweeks.category.domain.Category;
import tweeks.category.persistence.CategoryMapper;

@Service("CategoryServiceImpl")
public class CategoryServiceImpl implements CategoryService {
	
	@Resource(name="CategoryMapper")
	private CategoryMapper categoryMapper;

	@Override
	public List<Category> selectCategory(Category category) {
		return categoryMapper.selectCategory(category);
	}

	@Override
	public void createCategory(Category category) {
		List<Category> result = categoryMapper.selectSiblingCategory(category);
		if (result != null) {
			category.setOrderNo(result.size());
		}
		categoryMapper.createCategory(category);
	}

	@Override
	public void deleteCategory(Category category) {
		if (category.getParentId() == null || "".equals(category.getParentId())) {
			Category org = categoryMapper.selectCategory(category).get(0);
			category.setParentId(org.getParentId());
		}
		categoryMapper.deleteCategory(category);
		List<Category> sibling = categoryMapper.selectSiblingCategory(category);
		List<Category> children = categoryMapper.selectSubCategory(category);
		for (Category row : children) {
			row.setParentId(category.getParentId());
			sibling.add(row);
		}
		relocateSibling(sibling);
		categoryMapper.pushUp(category);
	}

	@Override
	public void modifyCategory(Category category) {
		if (category.getId() == null || "".equals(category.getId()))
			return;
		
		Category org = null;
		if (!category.isValid()) {
			org = categoryMapper.selectCategory(category).get(0);
			if (category.getParentId() != null && category.getId().equals(category.getParentId())) {
				category.setParentId(org.getParentId());
			}
			if (category.getName() == null || "".equals(category.getName())) {
				category.setName(org.getName());
			}
		}
		categoryMapper.modifyCategory(category);
	}
	
	@Override
	public List<Category> generateCategoryList(Category category) {
		List<Category> list = new ArrayList<Category>();
		list.add(category);
		addChildItem(list, category);
		return list;
	}
	
	public void addChildItem(List<Category> target, Category parent) {
		for (Category item : parent.getChildren()) {
			target.add(item);
			addChildItem(target, item);
		}
	}

	@Override
	public Category generateCategoryTree(Category category) {
		List<Category> org = categoryMapper.selectCategory(null);
		List<Category> result = categoryMapper.selectSubCategory(category);
		if (result != null && result.size() > 0) {
			for (Category row : result) {
				addChildNode(org, row, 1);
			}
		}
		category.setChildren(result);
		return category;
	}

	public void addChildNode(List<Category> org, Category parent, int level) {
		List<Category> children = new ArrayList<Category>();
		for (Category item : org) {
			if (item.getParentId() != null && item.getParentId().equals(parent.getId())) {
				item.setLevel(level);
				children.add(item);
				addChildNode(org, item, level + 1);
			}
		}
		parent.setChildren(children);
	}

	@Override
	public void relocateSibling(List<Category> category) {
	}
	
	@Override
	public void upwardRelocate(Category category) {
		if (category.getParentId() == null || "".equals(category.getParentId())) {
			Category result = categoryMapper.selectCategoryByPk(category);
			category.setParentId(result.getParentId());
		}
		List<Category> subCategory = categoryMapper.selectSubCategory(category);
		int currentOrderNo = 0;
		for (int i = 0; i < subCategory.size(); i++) {
			if (subCategory.get(i).getId().equals(category.getId())) {
				currentOrderNo = i;
			}
			subCategory.get(i).setOrderNo(i);
		}
		
		categoryMapper.upwardRelocate(category);
	}

	@Override
	public void downwardRelocate(Category category) {
		if (category.getParentId() == null || "".equals(category.getParentId())) {
			Category result = categoryMapper.selectCategoryByPk(category);
			category.setParentId(result.getParentId());
		}
		categoryMapper.downwardRelocate(category);
	}
}